﻿<?php
/**
 * This is the file to define Factory.
 * 
 * @author	Justin Yoo
 * @version	0.0.1.0001
 * @package	KYR.Core
 * 
 */

//	Checks if this file is called within the framework; otherwise terminates.
if (!defined("IS_FRAMEWORK") || !IS_FRAMEWORK)	header("Location: http://".$_SERVER["HTTP_HOST"]."/error/404");

/**
 * This is class Factory that defines global constants and handles database connection strings.
 *
 * Defines global constants and handles database connection strings.
 * 
 * @author	Justin Yoo
 * @package	KYR.Core
 * 
 */
class Factory
{
	private static $_dbConfig = null;
	
	/**
	 * Parses the configuration settings from config files.
	 *
	 * @access	public
	 * @param	string	$filename	Configuration file name.
	 * @exception					Throws an Exception while loading a configuration file.
	 * @static
	 *
	 */
	public static function ParseConfig($filename)
	{
		try
		{
			switch ($filename)
			{
				case "app.config":
					$config	= self::_LoadConfig($filename);
					self::_SetAppConfig($config);
					break;
				case "db.config":
					$config	= self::_LoadConfig($filename);
					self::_SetDbConfig($config);
					break;
			}
			return $config;
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Gets the database connection string.
	 *
	 * @access	public
	 * @param	string	$site	Type of site.
	 * @param	string	$db		Name of the database.
	 * @return	array			Returns the array of database connection string.
	 * @static
	 *
	 */
	public static function GetDb($site, $db)
	{
		return self::$_dbConfig[$site.$db];
	}
	
	/**
	 * Loads the configuration file.
	 *
	 * @access	private
	 * @param	string	$filename	Configuration file name.
	 * @return	object				Returns the XML object.
	 * @exception					Throws an Exception while loading the configuration file.
	 * @static
	 *
	 */
	private static function _LoadConfig($filename)
	{
		try
		{
			$config	= simplexml_load_file($filename);
			return $config;
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Sets constants gloablly used.
	 *
	 * @access	private
	 * @param	object	$config	Configuration object derived from an XML file.
	 * @static
	 *
	 */
	private static function _SetAppConfig($config)
	{
		//	Gets the configuration values.
		foreach ($config->xpath("add") as $add)
		{
			foreach ($add->attributes() as $attribute => $value)
				$attributes[$attribute] = (string) $value;

			$appConfig[$attributes["key"]] = $attributes["value"];
		}

		//	Defines the directory separator.
		if (!defined("SLASH"))
			define("SLASH", DIRECTORY_SEPARATOR);

		//	Defines the location of web sites.
		if (!defined("LOCAL_SITES"))
			define("LOCAL_SITES", $appConfig["localSites"]);

		if (!defined("DEV_SITES"))
			define("DEV_SITES", $appConfig["devSites"]);

		if (!defined("TEST_SITES"))
			define("TEST_SITES", $appConfig["testSites"]);

		if (!defined("LIVE_SITES"))
			define("LIVE_SITES", $appConfig["liveSites"]);

		//	Defines the physical root directory.
		if (!defined("ROOT_DIR"))
		{
			if (in_array($_SERVER["HTTP_HOST"], explode("|", LIVE_SITES)))
				define("ROOT_DIR", $appConfig["liveRoot"]);
			elseif (in_array($_SERVER["HTTP_HOST"], explode("|", TEST_SITES)))
				define("ROOT_DIR", $appConfig["testRoot"]);
			elseif (in_array($_SERVER["HTTP_HOST"], explode("|", DEV_SITES)))
				define("ROOT_DIR", $appConfig["devRoot"]);
			else
				define("ROOT_DIR", $appConfig["localRoot"]);
		}
		
		//	Defines the base reference.
		if (!defined("BASE_REF"))
		{
			if (in_array($_SERVER["HTTP_HOST"], explode("|", LIVE_SITES)))
				define("BASE_REF", $appConfig["liveBaseRef"]);
			elseif (in_array($_SERVER["HTTP_HOST"], explode("|", TEST_SITES)))
				define("BASE_REF", $appConfig["testBaseRef"]);
			elseif (in_array($_SERVER["HTTP_HOST"], explode("|", DEV_SITES)))
				define("BASE_REF", $appConfig["devBaseRef"]);
			else
				define("BASE_REF", $appConfig["localBaseRef"]);
		}

		//	Defines the base URL.
		if (!defined("BASE_URL"))
			define("BASE_URL", "http".($_SERVER["HTTPS"] == "off" ? null : "s")."://".$_SERVER["HTTP_HOST"]."/".(BASE_REF == "/" ? null : BASE_REF));

		//	Defines the file include paths
		if (!defined("CORE_PATH"))
			define("CORE_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["corePath"]).SLASH);

		if (!defined("MODULE_PATH"))
			define("MODULE_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["modulePath"]).SLASH);

		if (!defined("WIDGET_PATH"))
			define("WIDGET_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["widgetPath"]).SLASH);

		if (!defined("COMPONENT_PATH"))
			define("COMPONENT_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["componentPath"]).SLASH);

		if (!defined("HANDLER_PATH"))
			define("HANDLER_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["handlerPath"]).SLASH);

		if (!defined("TEMPLATE_PATH"))
			define("TEMPLATE_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["templatePath"]).SLASH);

		if (!defined("EXCEPTION_PATH"))
			define("EXCEPTION_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["exceptionPath"]).SLASH);

		if (!defined("LOG_PATH"))
			define("LOG_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["logPath"]).SLASH);

		if (!defined("MEDIA_PATH"))
			define("MEDIA_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["mediaPath"]).SLASH);

		if (!defined("RESOURCE_PATH"))
			define("RESOURCE_PATH", ROOT_DIR.str_replace("/", SLASH, $appConfig["resourcePath"]).SLASH);
		
		if (!defined("TEMPLATE_TAG_PREFIX"))
			define("TEMPLATE_TAG_PREFIX", $appConfig["templateTagPrefix"]);
	}
	
	/**
	 * Sets the database connection strings.
	 *
	 * @access	private
	 * @param	object	$config	Configuration object derived from an XML file.
	 * @static
	 *
	 */
	private static function _SetDbConfig($config)
	{
		//	Gets the configuration values.
		foreach ($config->xpath("add") as $add)
		{
			foreach ($add->attributes() as $attribute => $value)
				$attributes[$attribute] = (string) $value;

			$connectionStrings = array_filter(explode(";", $attributes["connectionString"]));
			for ($i = 0; $i < count($connectionStrings); $i++)
				$connectionStrings[$i] = explode("=", trim($connectionStrings[$i]));

			if (!empty($connectionStrings))
				self::$_dbConfig[$attributes["name"]] = array(
					str_replace(" ", "", $connectionStrings[0][0])	=> $connectionStrings[0][1],
					str_replace(" ", "", $connectionStrings[1][0])	=> $connectionStrings[1][1],
					str_replace(" ", "", $connectionStrings[2][0])	=> $connectionStrings[2][1],
					str_replace(" ", "", $connectionStrings[3][0])	=> $connectionStrings[3][1],
					str_replace(" ", "", $connectionStrings[4][0])	=> $connectionStrings[4][1]
					);
		}
	}
}
?>
